package modules.majiang.effect
{
	import com.greensock.TweenLite;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.geom.Point;
	
	import modules.GameDispatcher;
	import modules.majiang.MajiangEvent;
	import modules.majiang.data.TableDataModel;
	import modules.manager.SoundManager;

	/**
	 * 发牌特效数据
	 * @author xumin.xu
	 */
	public class FetchEffectData
	{
		private static var _cards:Array = null;

		private var tableDataModel:TableDataModel;

		public function FetchEffectData(tableDataModel:TableDataModel)
		{
			this.tableDataModel = tableDataModel;
		}

		private function get dispatcher():GameDispatcher
		{
			return GameDispatcher.instance;
		}

		/**
		 * 发牌
		 **/
		public function fetchCard(chairList:Array):void
		{
			var i:int;
			for (i = 0; i < chairList.length; i++)
			{
				var chairId:int = chairList[i];
				var position:int = tableDataModel.getPositionByChairId(chairId);

				var tiles:Array = tableDataModel.playerTiles[tableDataModel.getChairIdByPosition(position)];
				tiles.length = tableDataModel.handTilesNum + 1;

				var startPoint:Point = new Point(UIAllRefer.stage.stageWidth / 2, UIAllRefer.stage.stageHeight / 2);

				var data:Object = {position: position, cardIndex: tableDataModel.handTilesNum + 1};
				//获取牌
				dispatcher.dispatchEvent(new MajiangEvent(MajiangEvent.GET_FETCH_CARD, data));
				//获取牌飞往的信息
				dispatcher.dispatchEvent(new MajiangEvent(MajiangEvent.GET_FETCH_INFO, data));

				var card:MovieClip = data.card;
				var fetchToCard:MovieClip = data.fetchToCard;
				var targetPoint:Point = fetchToCard ? fetchToCard.localToGlobal(new Point()) : new Point();
				card.getChildByName("cardBitmap").visible = false;
				card.x = startPoint.x;
				card.y = startPoint.y;
				card.width = 29;
				card.height = 38;

				//使用特效类
				var tweenObj:Object = {};
				if (fetchToCard && card)
				{
					tweenObj = {x: targetPoint.x, y: targetPoint.y, width: fetchToCard.width, height: fetchToCard.height};
				}
				else
				{
					tweenObj = {x: targetPoint.x, y: targetPoint.y};
				}
				tweenObj.onStart = function(disO:DisplayObject):void
				{
					disO && (disO.visible = true);
				};
				tweenObj.onStartParams = [card];
				tweenObj.onComplete = fetchOver;
				tweenObj.onCompleteParams = [card, chairId];

				TweenLite.to(card, 0.5, tweenObj);
			}
		}

		private function fetchOver(disO:DisplayObject, chairId:int):void
		{
			disO && (disO.visible = false);

			var position:int = tableDataModel.getPositionByChairId(chairId);

			if (chairId == tableDataModel.sitChairId)
			{
				var isWin:Boolean = tableDataModel.isWin(tableDataModel.sitChairId);
				var isFold:Boolean = tableDataModel.isFold(tableDataModel.sitChairId);
				var isLose:Boolean = tableDataModel.isLose(tableDataModel.sitChairId);

				if (tableDataModel.sitChairId > 0 && isWin == false && isFold == false && isLose == false)
				{
					//允许玩家出牌
					tableDataModel.disableDiscard = true;
				}
				
				if (tableDataModel != null && UIAllRefer.instance.getGameUIbyTableId(tableDataModel.tableDataFromLobby.tableID).visible==true)
				{
					//播放摸牌的声音
					SoundManager.instance.drawTileSoundPlay();
				}
				
			}

			//抛出摸牌动画结束事件
			dispatcher.dispatchEvent(new MajiangEvent(MajiangEvent.FETCH_EFFECT_FINISH, {position: position}));
		}
	}
}
